package airmodel

import (
	"time"

	"github.com/astaxie/beego/config"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/mattn/go-sqlite3"
)

// RealData
type RealData struct {
	Id         int
	Mn         string `orm:"index"`
	Name       string
	Value      string
	Flag       string
	DataTime   time.Time `orm:"index"`
	InsertTime time.Time `orm:"index"`
}

// HisData
type HisData struct {
	Id         int
	Mn         string `orm:"index"`
	Name       string
	MinValue   string
	MaxValue   string
	AvgValue   string
	Flag       string
	DataTime   time.Time `orm:"index"`
	InsertTime time.Time `orm:"index"`
}

// MinData
type MinData HisData

// HourData
type HourData HisData

// DayData
type DayData HisData

//
func init() {
	beeconfig, err := config.NewConfig("ini", "../airconf/server.conf")
	if nil != err {
		panic(err)
	}

	orm.DefaultTimeLoc = time.Local
	// set default database
	// err := orm.RegisterDataBase("default", "sqlite3", "file:db/hjt212data.db")
	err = orm.RegisterDataBase("default", beeconfig.String("dbtype"), beeconfig.String("dbpath"))
	if err != nil {
		panic(err)
	}

	// register model
	orm.RegisterModel(new(RealData))
	orm.RegisterModel(new(MinData))
	orm.RegisterModel(new(HourData))
	orm.RegisterModel(new(DayData))

	// create table
	orm.RunSyncdb("default", false, true)
}

func GetRealData(mn, code string, pagenum int) (int64, []*RealData, int64) {
	var realdata []*RealData

	o := orm.NewOrm()
	qs := o.QueryTable("real_data")
	if len(mn) != 0 {
		qs = qs.Filter("mn__contains", mn)
	}
	if len(code) != 0 {
		qs = qs.Filter("name__contains", code)
	}

	all, err1 := qs.Count()
	if err1 != nil {
		return 0, realdata, 0
	}

	qs = qs.Limit(100, (pagenum-1)*100)
	num, err2 := qs.All(&realdata)
	if err2 != nil {
		return 0, realdata, 0
	}

	// for _, d := range realdata {
	// 	fmt.Println(d)
	// }

	return num, realdata, all
}

func GetMinData(mn, code string, pagenum int) (int64, []*MinData, int64) {
	var mindata []*MinData

	o := orm.NewOrm()
	qs := o.QueryTable("min_data")
	if len(mn) != 0 {
		qs = qs.Filter("mn__contains", mn)
	}
	if len(code) != 0 {
		qs = qs.Filter("name__contains", code)
	}

	all, err1 := qs.Count()
	if err1 != nil {
		return 0, mindata, 0
	}

	qs = qs.Limit(100, (pagenum-1)*100)
	num, err2 := qs.All(&mindata)
	if err2 != nil {
		return 0, mindata, 0
	}

	// for _, d := range mindata {
	// 	fmt.Println(d)
	// }

	return num, mindata, all
}

func GetHourData(mn, code string, pagenum int) (int64, []*HourData, int64) {
	var hourdata []*HourData

	o := orm.NewOrm()
	qs := o.QueryTable("hour_data")
	if len(mn) != 0 {
		qs = qs.Filter("mn__contains", mn)
	}
	if len(code) != 0 {
		qs = qs.Filter("name__contains", code)
	}

	all, err1 := qs.Count()
	if err1 != nil {
		return 0, hourdata, 0
	}

	qs = qs.Limit(100, (pagenum-1)*100)
	num, err2 := qs.All(&hourdata)
	if err2 != nil {
		return 0, hourdata, 0
	}

	// for _, d := range hourdata {
	// 	fmt.Println(d)
	// }

	return num, hourdata, all
}

func GetDayData(mn, code string, pagenum int) (int64, []*DayData, int64) {
	var daydata []*DayData

	o := orm.NewOrm()
	qs := o.QueryTable("day_data")
	if len(mn) != 0 {
		qs = qs.Filter("mn__contains", mn)
	}
	if len(code) != 0 {
		qs = qs.Filter("name__contains", code)
	}

	all, err1 := qs.Count()
	if err1 != nil {
		return 0, daydata, 0
	}

	qs = qs.Limit(100, (pagenum-1)*100)
	num, err2 := qs.All(&daydata)
	if err2 != nil {
		return 0, daydata, 0
	}

	// for _, d := range daydata {
	// 	fmt.Println(d)
	// }

	return num, daydata, all
}
